package com.jvm123.blog.controller

import com.jvm123.blog.dao.ApiRepos
import com.jvm123.blog.dao.CollectRepos
import com.jvm123.blog.entity.Api
import com.jvm123.blog.entity.Collect
import com.jvm123.blog.service.ApiService
import com.jvm123.blog.service.CollectService
import com.jvm123.blog.service.MenuService
import com.jvm123.blog.service.TermService
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.ModelAttribute
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping

/**
 *
 * @author yawn
 *     2019/10/6 18:54
 */
@Controller
@RequestMapping("admin")
class AdminController {

    Logger logger = LoggerFactory.getLogger(AdminController.class)

    @Autowired
    CollectService collectService
    @Autowired
    CollectRepos collectRepos
    @Autowired
    ApiService apiService
    @Autowired
    MenuService menuService
    @Autowired
    TermService termService
    @Autowired
    ApiRepos apiRepos

    @GetMapping("index")
    String index(Model model) {
        model.addAttribute("topMenus", menuService.getTopMenu())
        model.addAttribute("collects", collectRepos.findAll())
        model.addAttribute("categories", termService.findCategories())
        model.addAttribute("tags", termService.findTags())
        "admin/index"
    }

    @PostMapping("collect")
    String updateCollect(@ModelAttribute Collect collect) {
        collectService.update(collect)
        "redirect:/admin/index.html"
    }

    @PostMapping("collect/relate")
    String relateCollect(long id, String tag, String category) {
        collectService.relate(id, tag, category)
        "redirect:/collect/" + id + "/" + tag + category + ".html"
    }

    @PostMapping("collect/remove/relation")
    String removeRelation(long collectId, long postId) {
        collectService.removeRelation(collectId, postId)
        "redirect:/collect/" + collectId + ".html"
    }







    @GetMapping("api/add")
    String addApi(Long id, Model model) {
        Api apiEntity
        if (id == null || id <= 0) {
            apiEntity = new Api()
        } else {
            apiEntity = apiRepos.findOne(id)
        }
        model.addAttribute("apiEntity", apiEntity)
        "admin/add"
    }

    @PostMapping("api/add")
    String addApi(@ModelAttribute Api apiEntity) {
        apiEntity.setCreateDate(new Date())
        apiRepos.save(apiEntity)
        "admin/add"
    }

    @GetMapping("api/update")
    String updateApi(Long id, Model model) {
        Api apiEntity
        if (id == null || id <= 0) {
            apiEntity = new Api()
        } else {
            apiEntity = apiRepos.findOne(id)
        }
        model.addAttribute("apiEntity", apiEntity)
        "admin/update"
    }

    @PostMapping("api/update")
    String updateApi(@ModelAttribute Api apiEntity) {
        apiService.update(apiEntity)
        "admin/update"
    }
}
